var setBackground = function () {
    var width = $(window).width();
    var height = $(window).height();
    $('.com-table').css({
        width: width,
        height: height
    });
};

var lib = [
    {
        $elm: $('#B38A_TR0'),
        dbId: 8615214,
        flag: 1
    },
    {
        $elm: $('#B38B_TR0'),
        dbId: 8615218,
        flag: 1
    },
    {
        $elm: $('#B48A_TR0'),
        dbId: 8616102,
        flag: 1
    },
    {
        $elm: $('#B48B_TR0'),
        dbId: 8616126,
        flag: 1
    },
    {
        $elm: $('#B38A_TR1'),
        dbId: 8666898,
        flag: 1
    },
    {
        $elm: $('#B48A_TR1'),
        dbId: 8667034,
        flag: 1
    },
    {
        $elm: $('#B48B_TR1'),
        dbId: 8667036,
        flag: 1
    },
    {
        $elm: $('#Current_day'),
        dbId: '',
        flag: 2
    },
    {
        $elm: $('#Last_hour'),
        dbId: 'last_hour_quantity',
        flag: 1
    },
    {
        $elm: $('#Current_week'),
        dbId: 'week_quantity',
        flag: 1
    },
    {
        $elm: $('#Current_hour'),
        dbId: 'current_hour_quantity',
        flag: 1
    },
    {
        $elm: $('#Line_rate'),
        dbId: '',
        flag: 3
    }
];

var ts = Date.parse('2017-08-23 06:00:00');
var day_quantity = 0;
var getValue = function (res, item) {
    var result = 0,
        dbId = item.dbId,
        flag = item.flag,
        value = res[dbId] || 0;
    console.log(typeof res[dbId]);
    switch (flag) {
        case 1:
            result = value;
            break;
        case 2:
            day_quantity = 0;
            for (var i = 0; i < 7; i++) {
                if (res[lib[i].dbId]) {
                    day_quantity += parseInt(res[lib[i].dbId]);
                }
            }
            console.log(typeof value);
            result = day_quantity;
            break;
        case 3:
            var date = new Date();
            var hour = date.getHours();
            var minute = parseInt(date.getMinutes() * 10 / 6) / 100;
            var sumHour = (hour - 6) + minute;
            result = parseInt(day_quantity / sumHour * 10) / 10 + 'U/H';
            break;
    }
    return result;
};

var getData = function () {
    $.ajax({
        url: '/NEP/ALS/api/HeatTreatment?' + Date.now(),
        type: 'GET',
        dataType: 'json',
        success: function (res) {
            $.each(lib, function (index, item) {
                item.$elm.html(getValue(res, item));
            });
        }
    });
};

$(function () {
    setBackground();
    getData();
    setInterval(getData, 15000);
    $(window).on('resize', function () {
        setBackground();
    });
});